<?php

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    class Common{
        var $ipsclass;
        var $profile_url;
        var $custom_fields;
        function auto_run(){
            
        }
        function read_dir($path){
            if($dir = opendir($path)){
                $files = array();
                while (false !== ($file = readdir($dir))) {
                    //echo "$file\n";
                    $temp = explode(".", $file);
                    //echo  end($temp);
                    if ($file != "." && $file != ".." && end($temp) == "swf") {
                        $files[] = $file;
                    }
                }
                return $files;
            }else{
                return null;
            }
        }
        function top_event(){
            //$this->ipsclass->load_template('skin_home');
            $this->ipsclass->load_template('skin_regportal');
            //$this->ipsclass->load_language('lang_regportal');
            $where = " t.approved = 1 and t.forum_id = f.id and f.id = " . intval($this->ipsclass->vars['id_enews']);
            /*
             * update query
             */
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                     'from'     => array( 'topics' => 't' ),
                                                     'where'    => $where . " group by concat(t.tid) order by t.tid DESC",
                                                     'limit'         => array(0, 4),
                                                     'add_join' => array(0 => array(
                                                                                     'select'=>'p.*',
                                                                                     'from'   => array( 'posts' => 'p' ),
                                                                                     'where'    => 'p.topic_id = t.tid',
                                                                                     'type'   => 'left'
                                                                                    ),
                                                                         1 => array( 'select' => 'f.*',
                                                                                          'from'   => array( 'forums' => 'f' ),
                                                                                          'where'  => 'f.id=t.forum_id',
                                                                                          'type'   => 'left' )
                                                         )
                                                    )

                );
            $this->ipsclass->DB->exec_query();
            $event_news = array();
            while ($row = $this->ipsclass->DB->fetch_row()) {
                $row['rewrite_title'] = $this->filter_title($row['title']);
                $event_news[] = $row;
            }
            $vars = array();
            $vars['top_events_count'] = count($event_news);
            $vars['type'] = $this->ipsclass->vars['id_enews'];
            $vars['top_event'] = $event_news;
            $layout_comment = $this->ipsclass->compiled_templates['skin_regportal']->top_event($vars);
            return $layout_comment;
            
        }
        function load_stats(){
        $this->ipsclass->load_language('lang_boards');
        $this->ipsclass->load_template('skin_boards');
        return $this->my_show_totals();
    }
        function my_show_totals(){
            $stats_html = "";
            //tong so
            //hien co 
            //thanh vien moi nhat
            $this->ipsclass->load_language('lang_boards');
            $this->ipsclass->load_template('skin_boards');
            if ($this->ipsclass->vars['show_totals'])
            {
                if ( ! is_array( $this->ipsclass->cache['stats'] ) )
                {
                    $this->ipsclass->cache['stats'] = array();
                    $this->ipsclass->update_cache( array( 'name' => 'stats', 'array' => 1, 'deletefirst' => 1 ) );
                }
                $stats =& $this->ipsclass->cache['stats'];

                //-----------------------------------------
                // Update the most active count if needed
                //-----------------------------------------
                if ($this->users_online > $stats['most_count'])
                {
                    $stats['most_count'] = $this->users_online;
                    $stats['most_date']  = time();
                    $this->ipsclass->update_cache( array( 'name' => 'stats', 'array' => 1, 'deletefirst' => 1 ) );
                }
                //echo $this->users_online;exit();
                $most_time = $this->ipsclass->get_date( $stats['most_date'], 'LONG' );
                $this->ipsclass->lang['most_online'] = str_replace( "<#NUM#>" ,   $this->ipsclass->do_number_format($stats['most_count'])  , $this->ipsclass->lang['most_online'] );
                //$this->ipsclass->lang['most_online'] = str_replace( "<#DATE#>",                   $most_time                    , $this->ipsclass->lang['most_online'] );
                $total_posts = $stats['total_replies'] + $stats['total_topics'];
                $total_posts        = $this->ipsclass->do_number_format($total_posts);
                $stats['mem_count'] = $this->ipsclass->do_number_format($stats['mem_count']);
                $this->total_posts    = $total_posts;
                $this->total_members  = $stats['mem_count'];
                $link = $this->ipsclass->vars['profile_url']."/".strtolower($stats['last_mem_username'])."/";
                //echo $this->ipsclass->vars['profile_url'] ;exit();
                $total_online = $this->active_users();
                //exit();
                //var_dump($this->total_members);exit();
                //var_dump($this->active_users());exit();
                $this->ipsclass->lang['my_total_statisic'] = str_replace( "<#posts#>" ,   $total_online['TOTAL']       , $this->ipsclass->lang['my_total_statisic'] );
                $this->ipsclass->lang['my_total_statisic'] = str_replace( "<#reg#>"   , $this->total_members     , $this->ipsclass->lang['my_total_statisic'] );
                //echo $this->ipsclass->lang['my_total_statisic'];exit();
                //$this->ipsclass->lang['my_total_word_string'] = str_replace( "<#mem#>"   , $stats['last_mem_name'] , $this->ipsclass->lang['my_total_word_string'] );
                //$this->ipsclass->lang['my_total_word_string'] = str_replace( "<#link#>"  , $link                   , $this->ipsclass->lang['my_total_word_string'] );
                //echo $this->ipsclass->lang['my_total_word_string'];exit();
                //echo $this->ipsclass->member['id'];exit();
                //$this->ipsclass->lang['my_total_statisic'] = str_replace( "<#member#>"  ,  "Bạn" , $this->ipsclass->lang['my_total_word_string'] );
                //if(intval($this->ipsclass->member['id']) <=0 ){
                    
                    //$this->ipsclass->lang['my_total_word_string'] = str_replace( "<#member#>"  ,  "Bạn" , $this->ipsclass->lang['my_total_word_string'] );
                    
                    //$this->ipsclass->lang['my_total_word_string'] = str_replace( "<#back#>"  , $this->ipsclass->lang['back'] , $this->ipsclass->lang['my_total_word_string'] );
                    
                    //$this->ipsclass->lang['my_total_word_string'] = str_replace( "<#message#>"  , $this->ipsclass->lang['message'] , $this->ipsclass->lang['my_total_word_string'] );
                    
                //}else{
                    //$this->ipsclass->lang['my_total_word_string'] = intval($this->ipsclass->member['id']) > 0 ? str_replace( "<#member#>"  , $lik = isset ($this->ipsclass->member)? "<a href=".$this->ipsclass->vars['profile_url']."/".$this->ipsclass->member['name']."/>".$this->ipsclass->member['name']."</a>" : "Bạn" , $this->ipsclass->lang['my_total_word_string'] ): "Bạn";
                    //$this->ipsclass->lang['my_total_word_string'] = intval($this->ipsclass->member['id']) > 0 ? str_replace( "<#back#>"  , $this->ipsclass->lang['back'] , $this->ipsclass->lang['my_total_word_string'] ): $this->ipsclass->lang['comming'];
                    //$this->ipsclass->lang['my_total_word_string'] = intval($this->ipsclass->member['id']) > 0 ? str_replace( "<#message#>"  , "" , $this->ipsclass->lang['my_total_word_string'] ): $this->ipsclass->lang['message'];
                //}
                
                $stats_html .= $this->ipsclass->compiled_templates['skin_boards']->my_show_stats($this->ipsclass->lang['my_total_statisic']);
                //echo $stats_html;
            }
            return $stats_html;
        }
        
        function active_users()
	{   
                    //var_dump($this->ipsclass->vars['au_cutoff']);exit();
		$active = array( 'TOTAL'   => 0 ,
						 'NAMES'   => "",
						 'GUESTS'  => 0 ,
						 'MEMBERS' => 0 ,
						 'ANON'    => 0 ,
					   );
					   
		$stats_html = "";
		//echo $this->ipsclass->vars['au_cutoff']* 60;exit();
		if ( $this->ipsclass->vars['show_active'] )
		{
			if ($this->ipsclass->vars['au_cutoff'] == "")
			{
				$this->ipsclass->vars['au_cutoff'] = 15;
			}
			
			//-----------------------------------------
			// Get the users from the DB
			//-----------------------------------------
			
			$cut_off = $this->ipsclass->vars['au_cutoff'] * 60;
			$time    = time() - $cut_off;
			$rows    = array();
			$ar_time = time();
			
			if ( $this->ipsclass->member['id'] )
			{
				$rows = array( $ar_time.'.'.md5(microtime()) => array( 'id'			 => 0,
												  'login_type'   => substr($this->ipsclass->member['login_anonymous'],0, 1),
												  'running_time' => $ar_time,
												  'member_id'    => $this->ipsclass->member['id'],
												  'member_name'  => $this->ipsclass->member['members_display_name'],
												  'member_username'	=> $this->ipsclass->member['name'],
												  'member_group' => $this->ipsclass->member['mgroup'] ) );
                                                                                          
			}
			
			$this->ipsclass->DB->simple_construct( array( 'select' => 'id, member_id, member_name, member_username, login_type, running_time, member_group',
														  'from'   => 'sessions',
														  'where'  => "running_time > $time",
														  //'order'  => "running_time DESC" // Sort in PHP to avoid filesort in SQL
												 )      );
			
			
			$this->ipsclass->DB->simple_exec();
			
			//-----------------------------------------
			// FETCH...
			//-----------------------------------------

			while ( $r = $this->ipsclass->DB->fetch_row() )
			{
				$rows[ $r['running_time'].'.'.$r['id'] ] = $r;
			}

			krsort( $rows );
			//var_dump($rows);exit();
			//-----------------------------------------
			// Is this a root admin in disguise?
			// Is that kinda like a diamond in the rough?
			//-----------------------------------------
						
			$our_mgroups = array();
			
			if( isset($this->ipsclass->member['mgroup_others']) AND $this->ipsclass->member['mgroup_others'] )
			{
				$our_mgroups = explode( ",", $this->ipsclass->clean_perm_string( $this->ipsclass->member['mgroup_others'] ) );
			}
			
			$our_mgroups[] = $this->ipsclass->member['mgroup'];
			
			//-----------------------------------------
			// cache all printed members so we
			// don't double print them
			//-----------------------------------------
			
			$cached = array();
			//for($i=0;$i<=10;$i++) {
			foreach ( $rows as $result )
			{
				$last_date = $this->ipsclass->get_time( $result['running_time'] );
				
				//-----------------------------------------
				// Bot?
				//-----------------------------------------
				
				if ( strstr( $result['id'], '_session' ) )
				{
					//-----------------------------------------
					// Seen bot of this type yet?
					//-----------------------------------------
					
					$botname = preg_replace( '/^(.+?)=/', "\\1", $result['id'] );
					
					if ( ! $cached[ $result['member_name'] ] )
					{
						if ( $this->ipsclass->vars['spider_anon'] )
						{
							if ( in_array( $this->ipsclass->vars['admin_group'], $our_mgroups )  )
							{
								$active['NAMES'] .= "{$result['member_name']}*{$this->sep_char} \n";
							}
						}
						else
						{
							$active['NAMES'] .= "{$result['member_name']}{$this->sep_char} \n";
						}
						
						$cached[ $result['member_name'] ] = 1;
					}
					else
					{
						//-----------------------------------------
						// Yup, count others as guest
						//-----------------------------------------
						
						$active['GUESTS']++;
					}
				}
				
				//-----------------------------------------
				// Guest?
				//-----------------------------------------
				
				else if ( ! $result['member_id'] )
				{
					$active['GUESTS']++;
                                        
				}
				
				//-----------------------------------------
				// Member?
				//-----------------------------------------
				
				else
				{
                                    
					if ( empty( $cached[ $result['member_id'] ] ) )
					{
						$cached[ $result['member_id'] ] = 1;
                                                
						$result['member_name'] = $this->ipsclass->make_name_formatted( $result['member_name'], $result['member_group'] );
						
						if ($result['login_type'])
						{
							if ( (in_array( $this->ipsclass->vars['admin_group'], $our_mgroups )) and ($this->ipsclass->vars['disable_admin_anon'] != 1) )
							{
								$active['NAMES'] .= "<a href='{$this->ipsclass->profile_url}".strtolower($result['member_username'])."/' title='$last_date'>{$result['member_name']}</a>*{$this->sep_char} \n";
								$active['ANON']++;
							}
							else
							{
								$active['ANON']++;
							}
						}
						else
						{
							$active['MEMBERS']++;
							$active['NAMES'] .= "<a href='{$this->ipsclass->profile_url}".strtolower($result['member_username'])."/' title='$last_date'>{$result['member_name']}</a>{$this->sep_char} \n";
						}
					}
				}
			}
			//}
			//$active['NAMES'] = preg_replace( "/".preg_quote($this->sep_char)."$/", "", trim($active['NAMES']) );
                        //echo $active['MEMBERS'] . "@" . ceil($active['GUESTS']/2) . "@" . $active['ANON'];
                        //exit();
                        //var_dump($active);exit();
			$active['TOTAL'] = $active['MEMBERS'] + $active['GUESTS'] + $active['ANON'];
			$this->users_online = $active['TOTAL'];
			return $active;
		}
		
	}
        function filter_title($title){
            if($title == ""){
                return $title;
            }
            $title = strtolower($title);
            $title = $this->replace_unicode($title);
            $title = preg_replace('/[^a-zA-Z0-9]/','-',$title);
            $title = preg_replace('/\s*-+\s*/','-',$title);
            //$title = preg_replace('/\-+/','-',$title);
            return $title;
    }
        function replace_unicode($str){ 
        if(!$str) return ""; 
            $unicode = array( 
                'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ', 
                'd'=>'đ|Đ', 
                'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|E|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ', 
                'i'=>'í|ì|ỉ|ĩ|ị|I|Í|Ì|Ỉ|Ĩ|Ị', 
                'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|O|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ', 
                'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|U|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự', 
                'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Y|Ý|Ỳ|Ỷ|Ỹ|Ỵ ', 
                 
            ); 
            foreach($unicode as $nonUnicode=>$uni) 
                $str = preg_replace("/($uni)/i",$nonUnicode,$str); 
            return $str; 
    } 
        function my_top_poster(){
            $this->ipsclass->load_language('lang_stats');
            $this->ipsclass->load_template('skin_stats');
            //check load skin my_top_poster_header
            //if domain is forum then flag = true else
            $flag = false;
            $this->base_url = $this->ipsclass->base_url;
            $layout_top_poster = "";
            $layout_top_poster .= $this->ipsclass->compiled_templates['skin_stats']->top_poster_header();
            $time_high = time();
            $ids       = array();
            $time_low  = $time_high - (60*60*24);
            $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
            $domain = $protocol . '://' . $_SERVER['HTTP_HOST'];
            if($domain == $this->ipsclass->vars['forum_url'] || $domain == $this->ipsclass->vars['forum_pumper_url']){
                $flag = true;
            }
            //-----------------------------------------
            // Query the DB
            //-----------------------------------------

            foreach( $this->ipsclass->forums->forum_by_id as $id => $data )
            {
                if ( ! $data['inc_postcount'] )
                {
                    continue;
                }

                $ids[] = $id;
            }
            
            if( count( $ids ) )
            {
            $todays_posts = 0;

                $store = array();

                $this->ipsclass->DB->build_query( array( 'select' 	=> 'count(*) as cnt',
                                                                                                 'from' 	=> array( 'posts' => 'p' ),
                                                                                                 'where' 	=> "p.post_date > {$time_low} AND t.forum_id IN(".implode(",",$ids).")",
                                                                                                 'add_join' => array( 0 => array( 'from'	=> array( 'topics' => 't' ),
                                                                                                                                                                  'where'	=> 't.tid=p.topic_id',
                                                                                                                                                                  'type'	=> 'left' )
                                                                                                                                        )
                                                                                 ) 		);
                $this->ipsclass->DB->exec_query();

                $total_today = $this->ipsclass->DB->fetch_row();

                $this->ipsclass->DB->cache_add_query( 'stats_get_todays_posters', array( 'ids' => $ids, 'time_low' => $time_low ) );
                $this->ipsclass->DB->cache_exec_query();

                while ($r = $this->ipsclass->DB->fetch_row())
                {
                        $todays_posts += $r['tpost'];

                        $store[] = $r;
                }
                
                if ( $todays_posts )
                {
                    $i = 0;
                    foreach( $store as $info )
                    {		
                        $info['total_today_posts'] = $todays_posts;

                        if ($todays_posts > 0 and $info['tpost'] > 0)
                        {
                                $info['today_pct'] = sprintf( '%.1f',  ( $info['tpost'] / $total_today['cnt'] ) * 100  );
                                
                        }
                        $info['px'] = floor($info['today_pct']) . "px";
                        
                        $info['joined']  = $this->ipsclass->get_date( $info['joined'], 'JOINED' );

                        $info['posts'] = $this->ipsclass->do_number_format($info['posts']);
                        $info['tpost'] = $this->ipsclass->do_number_format($info['tpost']);
                        
                        $temp = $this->get_member_avartar($info['id']);
                        if($temp['avatar_location'] !=""){
                            $info['avatar_location'] = $this->ipsclass->vars['uploads_url_avatar'] . $temp['avatar_location'];
                        }else{
                            $info['avatar_location'] = $this->ipsclass->vars['style_url'] . "/images/profile/folder_profile_portal/no-avatar1.jpg";
                        }
                        $info['index'] = ++$i;
                        //var_dump($info);exit();
                        $info['members_display_name'] = $info['members_display_name'] ? $info['members_display_name'] : $this->ipsclass->lang['global_guestname'];
                        $info['member_url'] = $this->ipsclass->profile_url.strtolower($info['name'])."/";
                        if($flag){
                            $layout_top_poster .= $this->ipsclass->compiled_templates['skin_stats']->my_top_poster( $info );
                        }
                        else{
                            $layout_top_poster .= $this->ipsclass->compiled_templates['skin_stats']->top_poster_row( $info );
                        }
                        //$layout_top_poster .= $this->ipsclass->compiled_templates['skin_stats']->top_poster_row( $info );
                        
                        if($i==3){
                            break;
                        }
                    }
                }
                else
                {
                    //$layout_top_poster = $this->ipsclass->compiled_templates['skin_stats']->top_poster_no_info();
                }
            }
            else
            {
                //$layout_top_poster = $this->ipsclass->compiled_templates['skin_stats']->top_poster_no_info();
            }
            $layout_top_poster .= $this->ipsclass->compiled_templates['skin_stats']->top_poster_footer();
            //var_dump($store);exit();
            //echo $layout_top_poster;exit();
            return $layout_top_poster;
        }
        function get_member_avartar($id){
            if(intval($id) <= 0){
                return "";
            }
            $this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'member_extra', 'where' => "id = " . intval($id) ) );
            $oq = $this->ipsclass->DB->simple_exec();
            $this->ipsclass->DB->force_data_type = array( 'id'	 => 'int');
            $row = $this->ipsclass->DB->fetch_row($oq);
            return $row;
        }
        function load_right_content(){
            $this->load_init_skin();
            $files = $this->read_dir($this->ipsclass->vars['flash_diectory_path']."social/");       
            $random_file = $files[array_rand($files)];
            $random_flash_path = "/flash/social/";
            $layout_flash = $this->ipsclass->compiled_templates['skin_regportal']->skin_random__flash($random_flash_path, $random_file);
            $layout_top_event = $this->top_event();
            $output = $this->ipsclass->compiled_templates['skin_regportal']->skin_right_content($layout_flash, $layout_top_event);
            return $output;
        }
        function load_friend_list($mem = array(), $is_friend = FALSE){
            
            $this->load_init_skin();
            $friends = "";
            if(intval($mem['id']) <= 0){
                $mem = $this->ipsclass->member;
            }
            
            $mem = $this->personal_function_load_member($mem['id']);
            $mem = $this->personal_portal_set_information($mem);
            $friends_arr = $this->member_load_friends($mem, 0, 0);
            $friends_arr = (count($friends_arr) < 2) ? null : array_slice($friends_arr, 1, count($friends_arr) - 1);
            if (is_array($friends_arr)) {
                ksort($friends_arr);
            }
            $member = array();
            $member['id'] = $mem['id'];
            $member['total_friends'] = count($friends_arr);
            $member['approved_friends'] = $friends_arr;
            $friends = $this->ipsclass->compiled_templates['skin_regportal']->skin_friend_list($member);
            return $friends;
        }
        function load_friend_panel($member=array()){
            $this->load_init_skin();
            $member = $this->personal_function_load_member($member['id']);
            $profile_link = "";
            if($this->ipsclass->member['id'] == $member['id']){
                $profile_link = "{$this->ipsclass->vars['profile_url']}/thong-tin-ca-nhan.html";
                //$profile_link = "{$this->ipsclass->vars['profile_url']}/index.php?act=regportal&CODE=pf";
            }else{
                //$profile_link = "{$this->ipsclass->vars['profile_url']}/index.php?act=profile&CODE=guest-view-profile&profile_name={$member['name']}";
                $profile_link = "{$this->ipsclass->vars['profile_url']}/{$member['name']}/";
            }
            $member = $this->personal_portal_set_information($member);
            $member['profile_link'] = $profile_link;
            $friend_panel_layout = $this->ipsclass->compiled_templates['skin_regportal']->friend_panel($member);
            //echo $friend_panel_layout;exit();
            return $friend_panel_layout;
        }
        function load_avatar_info_content($member = array(), $is_friend = FALSE){
//            if(intval($member['id']) <= 0){
//                $member = $this->ipsclass->member;
//            }
            $friends = $this->member_load_all_friend($member, TRUE);
            $is_friend = false;
            if(intval($member['id']) != intval($this->ipsclass->member['id'])){
                if($friends != null ){
                    if(is_array($friends)){
                        foreach ($friends as $friend) {
                            if($member['id'] == $friend['id']){
                                $is_friend = true;
                                break;
                            }
                        }
                    }
                    
                }
            }else{
                $is_friend = true;
            }
            //echo "a:".$is_friend;exit();
            $info = "";
            $this->load_init_skin();
            $member['is_friend'] = $is_friend;
            $info = $this->ipsclass->compiled_templates['skin_regportal']->skin_avatar_form($member);
            return $info;
        }
        function load_avatar_message_content($member = array(), $is_friend = FALSE){
            if(intval($member['id']) <= 0){
                $member = $this->ipsclass->member;
            }
            $member['total_messages'] = $this->count_messages($member);
            
            $member['total_invited'] = $this->count_invited($member['id']);
            $message = "";
            $this->load_init_skin();
            $message = $this->ipsclass->compiled_templates['skin_regportal']->skin_messager_form($member);
            return $message;
        }
        function load_left_content($member = array(), $is_load_message = FALSE, $show_list_friend = FALSE, $show_friend_panel = FALSE, $show_bank = FALSE){
            //var_dump($member);exit();
            if(intval($member['id']) <= 0){
                $member = $this->ipsclass->member;
            }
            $left = "";
            $layout_mail = "";
            $layout_friend_list = "";
            $this->load_init_skin();
            $layout_avartar = $this->load_avatar_info_content($member);
            if($is_load_message && $this->ipsclass->member['id'] == $member['id']){
                $layout_mail = $this->load_avatar_message_content($member);
            }
            if($show_list_friend){
                $layout_friend_list = $this->load_friend_list($member);
            }
            if($show_friend_panel){
                $layout_friend_panel = $this->load_friend_panel($member);
            }
            //echo $layout_friend_panel;exit();
            if($show_bank){
                $layout_bank_panel = $this->load_bank_panel();
            }
            //echo $layout_bank_panel;exit();
            $left = $this->ipsclass->compiled_templates['skin_regportal']->skin_left_content($layout_avartar, $layout_mail, $layout_friend_list, $layout_friend_panel, $layout_bank_panel);
            
            return $left;
        }
        function load_init_skin(){
            $this->ipsclass->load_language('lang_regportal');
            $this->ipsclass->load_template('skin_regportal');
        }
        //-----------------------------------------
        // Create personal_function_load_member
        //-----------------------------------------
        function personal_function_load_member($member_id=0) {
            //-----------------------------------------
            // INIT
            //-----------------------------------------
            //require_once( ROOT_PATH . 'sources/action_public/topics.php' );
            //$topics = new topics();
            //$topics->ipsclass = & $this->ipsclass;

            $query = '';

            //-----------------------------------------
            // What do we have?
            //-----------------------------------------
            //echo $member_id;
            if (is_array($member_id)) {
                $query = 'm.id IN (' . implode(',', $member_id) . ')';
            } else {
                $query = 'm.id=' . intval($member_id);
            }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $this->ipsclass->DB->build_query(array('select' => 'm.*',
                'from' => array('members' => 'm'),
                'where' => $query,
                'add_join' => array(0 => array('select' => 'me.*',
                        'from' => array('member_extra' => 'me'),
                        'where' => 'me.id=m.id',
                        'type' => 'left'),
                    1 => array('select' => 'pp.*',
                        'from' => array('profile_portal' => 'pp'),
                        'where' => 'pp.pp_member_id=m.id',
                        'type' => 'left'),
                    2 => array('select' => 'g.*',
                        'from' => array('groups' => 'g'),
                        'where' => 'g.g_id=m.mgroup',
                        'type' => 'left'),
                    3 => array('select' => 's.location_1_id, s.location_2_id, s.location_1_type, s.location_2_type, s.running_time, s.location as sesslocation',
                        'from' => array('sessions' => 's'),
                        'where' => "s.member_id=m.id",
                        'type' => 'left'),
                    4 => array('select' => 'pc.*',
                        'from' => array('pfields_content' => 'pc'),
                        'where' => 'pc.member_id=m.id',
                        'type' => 'left'))));
            $this->ipsclass->DB->exec_query();

            if (is_array($member_id)) {
                while ($row = $this->ipsclass->DB->fetch_row()) {
                    //var_dump($row);exit();
                    $member[$row['id']] = $row;
                }
            } else {
                $member = $this->ipsclass->DB->fetch_row();
                //var_dump($member);
                $member['pp_setting_count_visitors'] = ( $member['pp_setting_count_visitors'] != 0 ) ? $member['pp_setting_count_visitors'] : 5;
                $member['pp_setting_count_comments'] = ( $member['pp_setting_count_comments'] != 0) ? $member['pp_setting_count_comments'] : 5;
                $member['pp_setting_count_friends'] = ( $member['pp_setting_count_friends'] != 0 ) ? $member['pp_setting_count_friends'] : 5;

                /*
                 * 
                 */
                $member['joined'] = date  ('Y m, d', $member['joined']);
                $member['last_visit'] = date  ('Y m, d', $member['last_visit']);
                //$now =  date("Y m, d, g:i a");
                $time = time();
                $member['now'] = date("Y m, d") ." - ".$this->ipsclass->get_date($time, 'SHORT');
                
                $member['total_topic'] = $this->count("topics", "tid", " starter_id = " . $member_id);
                //echo (time() - $member['joined'])/365/12/30/24/60/60;exit();
                $member['cusFullName'] = ($member['field_14'] != "" ? ($member['field_14'] . " "): "") . ($member['field_20'] != "" ? ($member['field_20'] . " "): "")  . $member['field_15'];
                
                //$this->ipsclass-> = ($member['field_14'] != "" ? ($member['field_14'] . " "): "") . ($member['field_20'] != "" ? ($member['field_20'] . " "): "")  . $member['field_15'];
                $member['avg_post'] = substr((time() - $member['joined'])/365/12/30/24/60/60, 0, 4);
                //field_4 : level
                //field_1: HP
                //field_2: MP
                //field_3: EXP
                //field_5: Danh vong
            }


            //-----------------------------------------
            // Add it to the cached list
            //-----------------------------------------
            //var_dump($member['cusFullName']);exit();
            return $member;
        }
        //-----------------------------------------
        // personal_portal_set_information
        //-----------------------------------------
        function personal_portal_set_information($member, $noids=0, $use_parsed=1) {
            return $this->ipsclass->member_set_information($member, $noids, $use_parsed);
        }
        function count($table_name, $column_count_name, $clause = null) {
            if (!empty($clause)) {
                $clause .= " and ";
            }
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => 'count(' . $column_count_name . ') as total',
                'from' => $table_name,
                'where' => $clause . ' 1 = 1; ')
            );
            $this->ipsclass->DB->exec_query();
            $row = $this->ipsclass->DB->fetch_row();
            return $row['total'];
            //return 0;
        }
        function format_member($member_id){
            $member = $this->personal_function_load_member($member_id);
            $member = $this->personal_portal_set_information($member);
            return $member;
        }
        //-----------------------------------------
        // member_load_friends
        //-----------------------------------------
        function member_load_friends($member, $per_page = "", $start = "", $clause = "") {

            //start paging
            if (empty($per_page) || $per_page == "" || $per_page == null) {
                $per_page = $this->ipsclass->vars['social_per_page_setting'];
            }
            if (empty($start) || $start == "" || $start == null) {
                $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
            }
            /*
             * Check clause (search mode)
             * 
             */
            $friends[] = array();

            $query = ' ( pf.friends_member_id = ' . intval($member['id']) . ' or pf.friends_friend_id = ' . intval($member['id']) . ')  AND pf.friends_approved = 1 AND  m.id <> ' . intval($member['id']) . ' group by concat( m.id )';

            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => ' pf.*',
                'from' => array('profile_friends' => 'pf'),
                'where' => $query,
                'limit' => array($start, $per_page),
                'add_join' => array(0 => array('select' => 'm.*',
                        'from' => array('members' => 'm'),
                        'where' => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                        'type' => 'left'),
                    1 => array('select' => 'me.*',
                        'from' => array('member_extra' => 'me'),
                        'where' => 'me.id=m.id',
                        'type' => 'left'),
                    2 => array('select' => 'pp.*',
                        'from' => array('profile_portal' => 'pp'),
                        'where' => 'pp.pp_member_id=m.id',
                        'type' => 'left'),
                    3 => array('select' => 'pc.*',
                        'from' => array('pfields_content' => 'pc'),
                        'where' => 'pc.member_id=m.id',
                        'type' => 'left')
                )
                    )
            );
            $this->ipsclass->DB->exec_query();

            while ($row = $this->ipsclass->DB->fetch_row()) {
                $friends[] = $this->ipsclass->member_set_information($row);
            }

            unset($member);
            
            return $friends;
        }
        function member_load_all_friend($member, $is_not_approved = FALSE){
            //echo $member['id'];exit();
            /*
             * Check clause (search mode)
             * 
             */
            $approved = 1;
            if($is_not_approved){
                $approved = 0;
                $query = $query = ' ( pf.friends_member_id = ' . intval($this->ipsclass->member['id']) . ' AND pf.friends_friend_id = ' . intval($member['id']) . '  ) OR ( pf.friends_member_id = ' . intval($member['id']) . ' AND pf.friends_friend_id = ' . intval($this->ipsclass->member['id']) . '  ) group by concat( m.id )';
                //$query = $query = ' ( pf.friends_member_id = ' . intval($member['id']) . ' or pf.friends_friend_id = ' . intval($member['id']) . ' ) group by concat( m.id )';
            }else{
                $query = ' ( pf.friends_member_id = ' . intval($member['id']) . ' or pf.friends_friend_id = ' . intval($member['id']) . ')  AND pf.friends_approved = '.$approved.' AND  m.id <> ' . intval($member['id']) . ' group by concat( m.id )';
            }
            $friends[] = array();

            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => ' pf.*',
                'from' => array('profile_friends' => 'pf'),
                'where' => $query,
                //'limit' => array($start, $per_page),
                'add_join' => array(0 => array('select' => 'm.*',
                        'from' => array('members' => 'm'),
                        'where' => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                        'type' => 'left'),
                    1 => array('select' => 'me.*',
                        'from' => array('member_extra' => 'me'),
                        'where' => 'me.id=m.id',
                        'type' => 'left'),
                    2 => array('select' => 'pp.*',
                        'from' => array('profile_portal' => 'pp'),
                        'where' => 'pp.pp_member_id=m.id',
                        'type' => 'left'),
                    3 => array('select' => 'pc.*',
                        'from' => array('pfields_content' => 'pc'),
                        'where' => 'pc.member_id=m.id',
                        'type' => 'left')
                )
                    )
            );
            $this->ipsclass->DB->exec_query();

            while ($row = $this->ipsclass->DB->fetch_row()) {
                $friends[] = $this->ipsclass->member_set_information($row);
            }

            unset($member);
            $friends = (count($friends) < 2) ? null : array_slice($friends, 1, count($friends) - 1);
            return $friends;
        }
        //-----------------------------------------
        // member_load_comment
        //-----------------------------------------
        function member_load_comment($member, $per_page= "", $start = "", $member_id = "", $sql_extra = null) {
            //start paging
            if (empty($per_page) || $per_page == "") {
                $per_page = $this->ipsclass->vars['social_per_page_setting'];
            }
            if (empty($start) || $start == "" || $start == null) {
                $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
            }

            //-----------------------------------------
            // Regenerate comments... by tuanhnm
            //-----------------------------------------
            //echo 'pc.comment_by_member_id=' . intval($member['id']) . " or(pc.comment_for_member_id = " . intval($member['id']) . " and pc.comment_by_member_id = " . intval($member_id) . " ) " . $sql_extra;exit();
            $this->ipsclass->DB->flush_query();

            $this->ipsclass->DB->build_query(array('select' => 'pc.*',
                'from' => array('profile_comments' => 'pc'),
                'where' => 'pc.comment_by_member_id=' . intval($member['id']) . " or(pc.comment_for_member_id = " . intval($member['id']) . " and pc.comment_by_member_id = " . intval($member_id) . " ) " . $sql_extra,
                'limit' => array($start, $per_page),
                'order' => 'pc.comment_date DESC',
                'add_join' => array(0 => array('select' => 'm.name, m.members_display_name, m.login_anonymous',
                        'from' => array('members' => 'm'),
                        'where' => 'm.id=pc.comment_by_member_id',
                        'type' => 'left'),
                    1 => array('select' => 'pp.*',
                        'from' => array('profile_portal' => 'pp'),
                        'where' => 'pp.pp_member_id=m.id',
                        'type' => 'left'),
                    2 => array('select' => 'me.*',
                        'from' => array('member_extra' => 'me'),
                        'where' => 'me.id=pc.comment_by_member_id',
                        'type' => 'left'))));


            $this->ipsclass->DB->exec_query();

            while ($row = $this->ipsclass->DB->fetch_row()) {
                $row['_comment_date'] = $this->ipsclass->get_date($row['comment_date'], 'SHORT');
                $row['_avatar'] = $this->ipsclass->get_avatar($row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type']);
                $row['_last_active'] = $this->ipsclass->get_date($row['last_activity'], 'SHORT');
                // Added by BabyWolf
                $row['name'] = strtolower($row['name']);

                if ($row['login_anonymous']{0} == '1') {
                    // Member last logged in anonymous

                    if ($this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon']) {
                        $row['_last_active'] = $this->ipsclass->lang['private'];
                    }
                }

                $row['comment_content'] = $this->ipsclass->txt_wordwrap($row['comment_content'], '19', ' ');

                $row = $this->personal_portal_set_information($row, 0, 0);

                $comments[] = $row;
            }
            //var_dump($comments);exit();
            return $comments;
        }

        function create_customize_paging($TOTAL_POSS, $PER_PAGE, $CUR_ST_VAL, $L_SINGLE = "", $L_MULTI = "", $BASE_URL = "", $is_profile = TRUE, $id="comment_paging"){
            $pages = $this->ipsclass->build_pagelinks(array('TOTAL_POSS' => $TOTAL_POSS,
                'PER_PAGE' => $PER_PAGE,
                'CUR_ST_VAL' => $CUR_ST_VAL,
                'L_SINGLE' => $L_SINGLE,
                'L_MULTI' => $L_MULTI,
                'BASE_URL' => $BASE_URL
                    )
            );
            if(!isset ($is_profile) || $is_profile == TRUE){
                if($pages != ""){
                    $pages = "<div class='paging' id='{$id}'>".$pages . "</div>";
                }
            }
            return $pages;
        }
        function count_messages($member){
            return $this->my_count("message_topics", "mt_owner_id", "mt_read = 0 and mt_owner_id = {$member['id']}") . "/" . $member['msg_total'];
        }
        function count_invited($member_id){
            return $this->my_count("profile_friends", "friends_id", "friends_approved = 0 and friends_friend_id = {$member_id} ") . "/" . $this->count("profile_friends", "friends_id", "friends_friend_id = {$member_id} ");
        }
        //-----------------------------------------
        // count
        //-----------------------------------------
        function my_count($table_name, $column_count_name, $clause = null) {
            if (!empty($clause) && $clause != "") {
                $clause .= " and ";
            }
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => 'count(' . $column_count_name . ') as total',
                'from' => $table_name,
                'where' => $clause . ' 1 = 1; ')
            );
            $this->ipsclass->DB->exec_query();
            $row = $this->ipsclass->DB->fetch_row();
            return $row['total'];
            //return 0;
        }
        function getByID($pid, $table_name = "", $id_column_name = ""){
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => 't.*',
                'from' => "{$table_name} t",
                'where' => "t.{$id_column_name} = {$pid};",
                //'limit'		=> array(0,1)
                        )
            );
            $this->ipsclass->DB->exec_query();
            $row = $this->ipsclass->DB->fetch_row();
            return $row;
    }
        function check_id($table_name, $id_column_name, $id, $select_column = ""){
            $str_select = "";
            if(!empty ($select_column) || $select_column != ""){
                $str_select = $select_column;
            }else{
                $str_select = $id_column_name;
            }

            $this->ipsclass->DB->simple_construct(array('select'=> $str_select,
                                                        'from'		=> $table_name,
                                                        'where'             => $id_column_name . ' = ' . $id,
                                                        'order'		=> $id_column_name,
                                                        'limit'		=> array(0,1)
                                                        )
                                                );

            $this->ipsclass->DB->simple_exec();
            error_reporting(E_ALL);
            while($result = $this->ipsclass->DB->fetch_row()){
                return $result[$id_column_name];
            }
            return -1;


        }
        function save_member($table_name,$member, $where){
            $this->ipsclass->DB->do_update($table_name, $member, $where);
        }
        function bash_uploaded_avatars($id)
        {
            foreach( array( 'jpg', 'jpeg', 'gif', 'png' ) as $ext )
            {

                if ( @file_exists( $this->ipsclass->vars['my_upload_url']."av-".$id.".".$ext ) )
                {
                    @unlink( $this->ipsclass->vars['my_upload_url']."av-".$id.".".$ext );
                }
            }
        }
        function check_friend($member_id, $friend_id, $is_approved = FALSE){
            
            $clause = "";
            $member_id = intval($member_id);
            $friend_id = intval($friend_id);
            $clause = " ( pf.friends_member_id = {$member_id} and pf.friends_friend_id = {$friend_id} ) or ( pf.friends_member_id = {$friend_id} and pf.friends_friend_id = {$member_id} ) ";
            if(isset ($is_approved)){
                if($is_approved){
                    $clause = "( ". $clause . " ) and pf.friends_approved = 1 ";
                }else{
                    $clause = "( ". $clause . " ) and pf.friends_approved = 0 ";
                }
            }
            
            $this->ipsclass->DB->simple_construct(array('select' => 'pf.*',
                'from' => 'profile_friends pf',
                'where' => $clause,
                'limit' => array(0, 1)
                    )
            );
            $this->ipsclass->DB->simple_exec();
            $id = -1;
            $result = $this->ipsclass->DB->fetch_row();
            if (!empty($result)) {
                $id = $result['friends_id'];
            }
            return $id;
        }
        function load_friends(){
            $layout_friends = "";
            $id = "";
            $friends[] = array();
            $pages = "";
            $stats = array();
            
            //$this->ipsclass->input['txtsearch'] = "nguyen";
            //$per_page = 1;
            $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
            $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
            //echo "start";
            if(isset ($this->ipsclass->input['txtsearch']) || isset ($this->ipsclass->input['key'])){
                //echo "search";exit();
                $name = $this->ipsclass->input['txtsearch'];
                require (KERNEL_PATH . "class_ajax.php");
                $xml_convert = new class_ajax();
                $xml_convert->ipsclass = & $this->ipsclass;
                $this->ipsclass->input['key'] = $xml_convert->convert_and_make_safe($this->ipsclass->input['key']);
                $key = $this->ipsclass->input['key'];
                $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
                $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
                if (!empty($key)) {
                    $name = $key;
                }
                $name = htmlspecialchars($name);
                require( ROOT_PATH . 'sources/action_public/search.php' );
                $search = new search;
                $search->ipsclass = & $this->ipsclass;
                $name = $search->filter_keywords($name, 0);
                $main_clause = "";
                $sql = "";
                $name = strtolower(trim($name));
                $has_member = -1;
                
                if (!empty($name)) {
                    //echo "sdsdsds";
                    $names = explode(" ", $name);
                    $has_member = $this->count("members", "name", "name = '$name'" );
                    if($has_member == 1){
                        $sql_like = "m.name = '$name'";
                    }else{
                        $names = explode(" ", $name);
                        if (count($names) == 1) {
                            $sql_like = "cf.field_14 like LOWER('%{$name}%') or cf.field_15 like LOWER('%{$name}%')";
                        } else {
                            $temp = implode(" ", array_slice($names, 1));
                            $main_clause = "( cf.field_14 = LOWER('" . $names[0] . "') and cf.field_15 = LOWER('" . $temp . "') ) and ";
                            $tem1 = array();
                            $tem2 = array();
                            for ($index = 0; $index < count($names); $index++) {
                                $tem1[] = "cf.field_14 like LOWER('%" . $names[$index] . "%')";
                                $tem2[] = "cf.field_15 like LOWER('%" . $names[$index] . "%')";
                            }
                            $str1 = implode(" or ", $tem1);
                            $str2 = implode(" or ", $tem2);
                            $sql_like = $str1 . " or " . $str2;
                        }
                        $sql_like = $main_clause . " " . $sql_like;
                    }
                    
                    $key = $name;
                    $this->ipsclass->DB->flush_query();
                    $this->ipsclass->DB->build_query(array('select' => 'cf.*',
                        'from' => array('pfields_content' => 'cf'),
                        'where' => $sql_like,
                        'limit' => array($start, $per_page),
                        'add_join' => array(0 => array(
                                'select' => 'm.*, m.name as username',
                                'from' => array('members' => 'm'),
                                'where' => 'm.id = cf.member_id',
                                'type' => 'left'
                            ),
                            1 => array('select' => 'pp.*',
                                'from' => array('profile_portal' => 'pp'),
                                'where' => 'pp.pp_member_id=m.id',
                                'type' => 'left'),
                            2 => array('select' => 'me.*',
                                'from' => array('member_extra' => 'me'),
                                'where' => 'me.id=m.id',
                                'type' => 'left')
                        )
                            )
                    );
                    $this->ipsclass->DB->exec_query();
                    $friends_friends = array();
                    while ($row = $this->ipsclass->DB->fetch_row()) {
                        $datas[] = $this->ipsclass->member_set_information($row);
                    }
                    if($has_member == 1){
                        $stats = $this->ipsclass->DB->simple_exec_query(array('select' => 'count(m.id) as count',
                            'from' => 'members m',
                            'where' => $sql_like
                                )
                        );
                    }else{
                        $stats = $this->ipsclass->DB->simple_exec_query(array('select' => 'count(cf.member_id) as count',
                            'from' => 'pfields_content cf',
                            'where' => $sql_like
                                )
                        );
                    }
                }
                
            }else{
                if(intval($this->ipsclass->input['friend_id']) > 0){
                    $id = intval($this->ipsclass->input['friend_id']);
                }else{
                    $id = intval($this->ipsclass->member['id']);
                }
                $member = $this->format_member($id);
                $query = " ( pf.friends_member_id = {$member['id']} or pf.friends_friend_id = {$member['id']})  AND pf.friends_approved = 1 AND  m.id <> {$member['id']} group by concat( m.id )";
                $stats = $this->ipsclass->DB->simple_exec_query(
                    array('select' => ' pf.*',
                        'from' => array('profile_friends' => 'pf'),
                        'where' => $query,
                        'limit' => array($start, $per_page),
                        'add_join' => array(0 => array('select' => 'm.*',
                                'from' => array('members' => 'm'),
                                'where' => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                                'type' => 'left'),
                            1 => array('select' => 'me.*',
                                'from' => array('member_extra' => 'me'),
                                'where' => 'me.id=m.id',
                                'type' => 'left'),
                            2 => array('select' => 'pp.*',
                                'from' => array('profile_portal' => 'pp'),
                                'where' => 'pp.pp_member_id=m.id',
                                'type' => 'left'),
                            3 => array('select' => 'pc.*',
                                'from' => array('pfields_content' => 'pc'),
                                'where' => 'pc.member_id=m.id',
                                'type' => 'left')
                        )
                    )
                );
                $datas = $this->member_load_friends($member);
                $datas = (count($datas) < 2) ? null : array_slice($datas, 1, count($datas) - 1);
                if (is_array($datas)) {
                    ksort($datas);
                }
            }
            $pages = $this->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],"#", TRUE, "all_friend_paging");
            $friends['friend_id'] = $member['id'];
            $friends['pages'] = $pages;
            $friends['total_friends'] = count($datas);
            $friends['owned_id'] = $this->ipsclass->member['id'];
            $friends['datas'] = $datas;
            
            $layout_friends = $this->ipsclass->compiled_templates['skin_regportal']->content_wg_load_all_friends($friends);
            //echo $layout_friends;
            return $layout_friends;
        }
        function parseLastVisit(){
            if (!$this->ipsclass->member['id']) {
            $this->ipsclass->input['last_visit'] = time();
            }
            if ($this->ipsclass->vars['ipbli_usertype'] != 'username') {
                $this->ipsclass->lang['qli_name'] = $this->ipsclass->lang['email_address'];
            }
            $this->ipsclass->member['last_visit'] = $this->ipsclass->get_date($this->ipsclass->member['last_visit'], 'SHORT');
            if ($this->ipsclass->member['bday_year'] != "0") {
                $this->ipsclass->yearold = date("Y", time()) - $this->ipsclass->member['bday_year'];
            } else {
                $this->ipsclass->yearold = "Chưa rỏ";
            }
        }
        function total_friend($member_id){
            $member_id = intval($member_id);
            $query = " ( pf.friends_member_id = {$member_id} or pf.friends_friend_id = {$member_id} ) AND pf.friends_approved = 1 AND  m.id <> {$member_id} group by concat( m.id )";
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => ' pf.*',
                'from' => array('profile_friends' => 'pf'),
                'where' => $query,
                'add_join' => array(0 => array('select' => 'm.*',
                        'from' => array('members' => 'm'),
                        'where' => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                        'type' => 'left'),
                    1 => array('select' => 'me.*',
                        'from' => array('member_extra' => 'me'),
                        'where' => 'me.id=m.id',
                        'type' => 'left'),
                    2 => array('select' => 'pp.*',
                        'from' => array('profile_portal' => 'pp'),
                        'where' => 'pp.pp_member_id=m.id',
                        'type' => 'left'),
                    3 => array('select' => 'pc.*',
                        'from' => array('pfields_content' => 'pc'),
                        'where' => 'pc.member_id=m.id',
                        'type' => 'left')
                )
                    )
            );
            $this->ipsclass->DB->exec_query();
            return $this->ipsclass->DB->get_num_rows();

        }
        function load_bank_panel(){
            
            $bank_panel_layout = "";
            $this->ipsclass->load_language('lang_bank');
            
            //echo($this->ipsclass->compiled_templates['skin_regportal']->bank_panel());exit();
            if(intval($this->ipsclass->member['id']) > 0){
                $bank_panel_layout = $this->ipsclass->compiled_templates['skin_regportal']->bank_panel();
            }
            
            return $bank_panel_layout;
        }
        function load_friend($member_name){
            $member          = array();
            if ( ! $member_name ){
                return null;
            }
            $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                        'from'		=> 'members',
                                                        'where'		=> "name='".$member_name."'"
                                                        )
                                                );
            $this->ipsclass->DB->simple_exec();
            $result = $this->ipsclass->DB->fetch_row();
            $member_id = $result['id'];
            if(intval($member_id) <= 0){
                return null;
            }
            if ( $member['pp_setting_count_friends'] > 0 ){
                $this->ipsclass->DB->build_query( array( 'select'   => 'pf.*',
                                                         'from'     => array( 'profile_friends' => 'pf' ),
                                                         'where'    => 'pf.friends_member_id='.$member_id. ' AND pf.friends_approved=1' . $sql_extra,
                                                         'limit'    => array( $start, $per_page ),
                                                         'add_join' => array( 0 => array( 'select' => 'm.*',
                                                                                                      'from'   => array( 'members' => 'm' ),
                                                                                                      'where'  => 'm.id=pf.friends_friend_id',
                                                                                                      'type'   => 'left' ),
                                                                              1 => array( 'select' => 'pp.*',
                                                                                                      'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                      'where'  => 'pp.pp_member_id=m.id',
                                                                                                      'type'   => 'left' ),
                                                                              2 => array( 'select' => 'me.*',
                                                                                                      'from'   => array( 'member_extra' => 'me' ),
                                                                                                      'where'  => 'me.id=pf.friends_friend_id',
                                                                                                      'type'   => 'left' ) ) ) );

                $this->ipsclass->DB->exec_query();

                while( $row = $this->ipsclass->DB->fetch_row() )
                {
                    $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                    $row['_avatar']        = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                    $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'DATE' );
                    $row['_location']	   = $row['location'] ? $row['location'] : $this->ipsclass->lang['no_info'];
                    $row = $this->common->personal_portal_set_information( $row, 0, 0 );
                    $row['members_display_name_short'] = $this->ipsclass->txt_truncate( $row['members_display_name'], 13 );
                    $friends[] = $row;
                }
            }
            $member = $this->format_member($member_id);
            return $member;
        }
        
        function get_first_attachment($id){
            if(intval($id) > 0){
                 $id = $id;
            }else{
                return null;
            }
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => '*',
                'from' => 'attachments at',
                'where' => ' at.attach_rel_id =  ' . $id ." order by at.attach_id ASC")
            );
            $this->ipsclass->DB->exec_query();
            $row = $this->ipsclass->DB->fetch_row();
            return $row;
        }
        function format_image_urls($row = array()){
            
            if($row['topic_hasattach'] > 0){
                $attachs = array();
                $this->ipsclass->DB->flush_query();
                $this->ipsclass->DB->build_query(array('select' => '*',
                    'from' => 'attachments at',
                    'where' => ' at.attach_rel_id =  ' . $row['pid'] ." order by at.attach_id DESC ")
                );
                $this->ipsclass->DB->exec_query();
                while($row2 = $this->ipsclass->DB->fetch_row()){
                    $row['main_image'] = $this->ipsclass->vars['uploads_url'].'/'. $row2['attach_location'];
                    $row['thumb_image'] = $this->ipsclass->vars['uploads_url'].'/thumbs/'. $row2['attach_location'];
                    break;
                }
                
                
            }else{
                $row['main_image'] = $this->get_first_image($row['post']);
                $row['thumb_image'] = $this->get_first_image($row['post']);
            }
            //var_dump($row);
            return $row;
        }
        function get_all_image($string_post){
            if($string_post == ""){
                return "";
            }
            $event_images = array();
            //preg_match_all('/src="([^"]*)"/',$string_post, $event_images);
            preg_match_all('/<img [^>]*src=["|\']([^"|\']+)/i', $string_post, $event_images);
            if(count($event_images) == 2){
                return $event_images[1];
            }
            return null;
        }
        function get_first_flash($string_post){
            if($string_post == ""){
                return "";
            }

            $flashs = array();
            preg_match_all('/(<object.*>)(.*)(<\/object>)/imxsU',$string_post, $flashs);
            if(count($flashs) > 0){
                return $flashs[0];
            }else{
                return "";
            }
            return "";
        }
        function get_all_attachment($id){
            if(intval($id) > 0){
                 $id = $id;
            }else{
                return null;
            }
            
            $attachs = array();
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => '*',
                'from' => 'attachments at',
                'where' => ' at.attach_rel_id =  ' . $id ." order by at.attach_id ASC")
            );
            $this->ipsclass->DB->exec_query();
            while($row = $this->ipsclass->DB->fetch_row()){
                $path = $this->ipsclass->vars['uploads_url']. "/".$row['attach_location'];
                $attachs[] = $path;
            }
            //var_dump($attachs);
            return $attachs;
        }
        function get_first_image($string_post){
            if($string_post == ""){
                return "";
            }

            $event_images = array();
            preg_match_all('/<img [^>]*src=["|\']([^"|\']+)/i',$string_post, $event_images);

            if(count($event_images) > 0){
                foreach ($event_images[1] as $key=>$value) {
                    return $value;
                }
            }else{
                return "";
            }
            return "";
        }
        function parseMemberProfile($member = array()){
            require_once( ROOT_PATH . 'sources/classes/class_custom_fields.php' );
            $this->custom_fields = new custom_fields($this->ipsclass->DB);

            $this->custom_fields->member_id = $this->ipsclass->member['id'];
            $this->custom_fields->cache_data = $this->ipsclass->cache['profilefields'];
            $this->custom_fields->admin = intval($this->ipsclass->member['g_access_cp']);
            $this->custom_fields->supmod = intval($this->ipsclass->member['g_is_supmod']);
            if ($this->custom_fields) {
                $this->custom_fields->member_data = $this->ipsclass->member;
                $this->custom_fields->admin = intval($this->member['g_access_cp']);
                $this->custom_fields->supmod = intval($this->member['g_is_supmod']);
                $this->custom_fields->member_id = $this->member['id'];
                $this->custom_fields->init_data();
                $this->custom_fields->parse_to_view(1);
                if (count($this->custom_fields->out_fields)) {
                    foreach ($this->custom_fields->out_fields as $i => $data) {
                        //if ( $data )
                        //{
                        $member['custom_fields'] .= "\n" . $this->custom_fields->method_format_field_for_topic_view($i);
                        //}
                    }
                    $member['dkm_Level'] = $this->custom_fields->field_data['Level'];
                    $member['dkm_Icon'] = $this->custom_fields->field_data['dkm_Icon'];
                    $member['dkm_Reputation'] = $this->custom_fields->field_data['dkm_Reputation'];
                    $member['dkm_HP'] = $this->custom_fields->field_data['HP'];
                    $member['dkm_MP'] = $this->custom_fields->field_data['MP'];
                    $member['dkm_EXP'] = $this->custom_fields->field_data['EXP'];
                    $member['dkm_Cash'] = $this->custom_fields->field_data['dkm_Cash'];
                }
            }
            return $member;
        }
        function getCategory($type){
            $cats= array();
            $where = "f.parent_id = {$type} order by f.name DESC";
            $this->ipsclass->DB->build_query( array('select' => 'f.id, f.name',
                                                'from'   => 'forums f',
                                                'where'    => $where
                                                ) 
                                        );
            $this->ipsclass->DB->exec_query();
            while($row = $this->ipsclass->DB->fetch_row()){
                if(intval($row['id']) > 0 ){
                    $row['rewrite_name'] = $this->filter_title($row['name']);
                    //$row['rewrite_date'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['post_date']);
                    $cats[] = $row;
                }
            }
            return $cats;
        }
        //kiem tra thanh vien da tung mua san pham hay chua
//        function checkBuyed(){
//            
//        }
    }
    
?>
